package pl.edu.pw.polygen.ui.file;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import pl.edu.pw.polygen.beans.ProjectBean;
import pl.edu.pw.polygen.core.project.ProjectService;
import pl.edu.pw.polygen.ui.file.upload.FileUploadPanel;
import pl.edu.pw.polygen.util.ApplicationHelper;

import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;

@Component("fileForm")
@Scope("prototype")
public class FileFormImpl extends GridLayout implements FileForm{

	private static final long serialVersionUID = 229575251353716766L;
	
	private static final String PROJECT_NAME_PROPERTY = "name";
	private static final String PROJECT_ID_PROPERTY = "id";
	
	@Autowired
	private ProjectService projectService;
	
	private static final String COMMON_FIELD_WIDTH = "40em";
		
	private static final int rowsAmount = 4;
	private static final int columnsAmount = 2;
	
	private ComboBox projectCombo;
	private TextField fileName;
	private TextArea fileDescription;
	private Label polyLabel = new Label("*.poly file");
	private FileUploadPanel fileUploadPanel;
	
	public FileFormImpl(){
		super(columnsAmount,rowsAmount);
		setSpacing(true);
	}
	
	public void initialize(){
		removeAllComponents();
		addComponent(new Label("Project: "),0,0);
		addComponent(getProjectsCombo(),1,0);
		addComponent(new Label("Name: "), 0, 1);
		addComponent(getProjectNameField(),1,1);
		addComponent(new Label("Description: "), 0, 2);
		addComponent(getProjectDescriptionField(),1,2);
		addComponent(polyLabel,0,3);
		addComponent(getUploadPanel());
	}
	
	private ComboBox getProjectsCombo(){
		List<ProjectBean> projectList = projectService.getAllUserProjects(ApplicationHelper.getLoggedUser().getUsername());
		BeanItemContainer<ProjectBean> projectContainer = new BeanItemContainer<ProjectBean>(ProjectBean.class);
		if(projectList!=null){
			for(ProjectBean projectBean : projectList){
				projectContainer.addItem(projectBean);
			}
		}
		projectCombo = new ComboBox();
		projectCombo.setContainerDataSource(projectContainer);
		projectCombo.setRequiredError("Please select project");
		projectCombo.setItemCaptionPropertyId(PROJECT_NAME_PROPERTY);
		projectCombo.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_PROPERTY);
		
		projectCombo.setWidth(COMMON_FIELD_WIDTH);
		projectCombo.setRequired(true);
		return projectCombo;
	}
	
	private TextField getProjectNameField(){
		if(fileName==null){
			fileName = new TextField();
			fileName.setWidth(COMMON_FIELD_WIDTH);
			fileName.setRequired(true);
			fileName.setRequiredError("Please enter a project name");
		}
		return fileName;
	}
	
	private TextArea getProjectDescriptionField(){
		if(fileDescription==null){
			fileDescription = new TextArea();
			fileDescription.setWidth(COMMON_FIELD_WIDTH);
		}
		return fileDescription;
	}
	
	private FileUploadPanel getUploadPanel(){
		fileUploadPanel = new FileUploadPanel();
		return fileUploadPanel;
	}

	@Override
	public boolean isValid() {
		fileName.commit();
		projectCombo.commit();
		fileName.validate();
		projectCombo.validate();
		return (fileName.isValid() && projectCombo.isValid());
	}


	@Override
	public String getFileName() {
		return (String) fileName.getValue();
	}

	@Override
	public void setFileName(String name) {
		fileName.setValue(name);
	}

	@Override
	public String getFileDescription() {
		return (String) fileDescription.getValue();
	}

	@Override
	public void setFileDescription(String description) {
		fileDescription.setValue((description==null)? "":description);
	}


	@Override
	public Long getProjectId() {
		ProjectBean value = (ProjectBean)projectCombo.getValue();
		return value.getId();
	}

	@Override
	public void setProjectName(String projectName) {
		Object [] items =  projectCombo.getItemIds().toArray();
		if(items != null){
			for(Object item : items){
				ProjectBean projectBean = (ProjectBean)item;
				if(projectBean.getName().equals(projectName)){
					projectCombo.setValue(item);
				}
			}
		}
	}

	@Override
	public File getUploadFile() {
		return fileUploadPanel.getFile();
	}
	
}
